import * as api from "../api/api";
import request from "../utils/request";
import { pageSize } from "../utils/common";

export default {
  namespace: "item",
  state: {
    storeId: 1,
    stores: { items: [] },
    coupon: {},
    itemCoupons: { items: [] },
    itemCategories: { items: [] },
    subItemCategories: { items: [] },
    itemCategory: {},
    item: {},
    loaded: false,
    items: { totalCount: 0, items: [] },
    itemSearchs: {
      storeId: "",
      keywords: "",
      pageIndex: 1,
      pageSize: pageSize,
      itemCategoryId: "",
      recommendCategoryId: "",
      isHot: "",
      isSale: "",
      isGroup: "",
      isTimeLimit: "",
      isWholesale: "",
      isBargain: "",
      isReleasePoints: ""
    },
    recommendCategory: {},
    recommendCategories: { totalCount: 0, items: [] },
    itemSpecs: { items: [] },
    itemSpec: {},
    itemComment: {},
    itemComments: { totalCount: 0, items: [] },
    itemCommentSearchs: { pageIndex: 1, pageSize: pageSize },
    itemSkus: { items: [] },
    itemSku: {},
    itemSkuCategory: {},
    itemSkuSpecs: { items: [] },
    businessScope: {},
    businessScopes: { totalCount: 0, items: [] },
    businessScopeSearchs: { keywords: "", pageIndex: 1, pageSize: pageSize },
    dividend: {},
    dividends: { totalCount: 0, items: [] },
    dividendSearchs: { pageIndex: 1, pageSize },
    releasePointRules: { items: [] }
  },
  effects: {
    /**
     * 获取店铺列表
     * @param param0
     * @param param1
     */
    *getStores({ payload }, { call, put }) {
      const { callback } = payload;
      const stores: api.AbpApplicationServicesDtoListResultDtoSingleUserMallStoresVOStoreVO = yield request(
        api.ManagestoreManageApiFetchParamCreactor.managestoreManageGetStoresAsync(
          {}
        )
      );
      const storeId = stores.items && stores.items[0].id;
      yield put({ type: "save", payload: { stores } });
      yield put({ type: "save", payload: { storeId } });
      callback && callback(true);
    },
    /**
     * 获取释放体验金规则列表
     * @param param0
     * @param param1
     */
    *getReleasePointRules({ payload }, { call, put }) {
      const releasePointRules = yield request(
        api.ManagemallManageApiFetchParamCreactor.managemallManageGetReleasePointRulesAsync(
          {}
        )
      );
      yield put({ type: "save", payload: { releasePointRules } });
    },
    /**
     * 获取商品分类
     * @param param0
     * @param param1
     */
    *getItemCategories({ payload }, { call, put }) {
      const { storeId } = payload;
      if (storeId == "请选择") {
        return;
      }
      const formStoreId = storeId;
      const itemCategories = yield request(
        api.ManageitemManageApiFetchParamCreactor.manageitemManageGetItemCategoriesAsync(
          { formStoreId }
        )
      );
      yield put({ type: "save", payload: { itemCategories } });
    },
    /**
     * 获取二级商品分类
     * @param param0
     * @param param1
     */
    *getSubItemCategories({ payload }, { call, put }) {
      const { storeId, itemCategoryId } = payload;
      if (storeId == "请选择") return;
      const formStoreId = storeId;
      const formItemCategoryId = itemCategoryId;
      const subItemCategories = yield request(
        api.ManageitemManageApiFetchParamCreactor.manageitemManageGetItemCategoriesAsync(
          { formStoreId, formItemCategoryId }
        )
      );
      yield put({ type: "save", payload: { subItemCategories } });
    },
    /**
     * 保存商品分类
     * @param param0
     * @param param1
     */
    *saveItemCategory({ payload }, { call, put }) {
      const { form, callback } = payload;
      yield request(
        api.ManageitemManageApiFetchParamCreactor.manageitemManageSaveItemCategoryAsync(
          { form }
        )
      );
      callback && callback(true);
    },
    /**
     * 移除商品分类
     * @param param0
     * @param param1
     */
    *removeItemCategory({ payload }, { call, put }) {
      const { form, callback } = payload;
      yield request(
        api.ManageitemManageApiFetchParamCreactor.manageitemManageRemoveItemCategoriesAsync(
          { form }
        )
      );
      callback && callback(true);
    },
    /**
     * 上移商品分类
     * @param param0
     * @param param1
     */
    *moveUpItemCategory({ payload }, { call, put }) {
      const { form, callback } = payload;
      yield request(
        api.ManageitemManageApiFetchParamCreactor.manageitemManageMoveUpItemCategoryAsync(
          { form }
        )
      );
      callback && callback(true);
    },
    /**
     * 下移商品分类
     * @param param0
     * @param param1
     */
    *moveDownItemCategory({ payload }, { call, put }) {
      const { form, callback } = payload;
      yield request(
        api.ManageitemManageApiFetchParamCreactor.manageitemManageMoveDownItemCategoryAsync(
          { form }
        )
      );
      callback && callback(true);
    },
    /**
     * 上移商品
     * @param param0
     * @param param1
     */
    *moveUpItem({ payload }, { call, put }) {
      const { form, callback } = payload;
      yield request(
        api.ManageitemManageApiFetchParamCreactor.manageitemManageMoveUpItemAsync(
          { form }
        )
      );
      callback && callback(true);
    },
    /**
     * 下移商品
     * @param param0
     * @param param1
     */
    *moveDownItem({ payload }, { call, put }) {
      const { form, callback } = payload;
      yield request(
        api.ManageitemManageApiFetchParamCreactor.manageitemManageMoveDownItemAsync(
          { form }
        )
      );
      callback && callback(true);
    },
    /**
     * 获取商品详情
     * @param param0
     * @param param1
     */
    *getItem({ payload }, { call, put }) {
      const { id, callback } = payload;
      const formId = id;
      const item = yield request(
        api.ManageitemManageApiFetchParamCreactor.manageitemManageGetItemAsync({
          formId
        })
      );
      yield put({ type: "save", payload: { item, loaded: true } });
      callback && callback(item);
    },
    /**
     * 保存商品
     * @param param0
     * @param param1
     */
    *saveItem({ payload }, { call, put }) {
      const { form, callback } = payload;
      const item = yield request(
        api.ManageitemManageApiFetchParamCreactor.manageitemManageSaveItemAsync(
          { form }
        )
      );
      yield put({ type: "save", payload: { item, loaded: true } });
      callback && callback(true);
    },
    /**
     * 移除商品
     * @param param0
     * @param param1
     */
    *removeItem({ payload }, { call, put }) {
      const { form, callback } = payload;
      yield request(
        api.ManageitemManageApiFetchParamCreactor.manageitemManageRemoveItemAsync(
          { form }
        )
      );
      callback && callback(true);
    },
    /**
     * 设置商品为置顶
     * @param param0
     * @param param1
     */
    *setItemToPop({ payload }, { call, put }) {
      const { form, callback } = payload;
      yield request(
        api.ManageitemManageApiFetchParamCreactor.manageitemManageSetItemIsPop({
          form
        })
      );
      callback && callback(true);
    },
    /**
     * 取消商品为置顶
     * @param param0
     * @param param1
     */
    *setItemToNotPop({ payload }, { call, put }) {
      const { form, callback } = payload;
      yield request(
        api.ManageitemManageApiFetchParamCreactor.manageitemManageSetItemIsNotPop(
          { form }
        )
      );
      callback && callback(true);
    },
    /**
     * 获取商品列表
     * @param param0
     * @param param1
     */
    *getItems({ payload }, { call, put }) {
      const {
        storeId,
        keywords,
        pageIndex,
        pageSize,
        itemCategoryId,
        recommendCategoryId,
        isHot,
        isSale,
        isGroup,
        isTimeLimit,
        isWholesale,
        isBargain,
        isReleasePoints
      } = payload;
      if (storeId == "请选择") return;
      const formStoreId = storeId,
        formKeywords = keywords,
        formItemCategoryId = itemCategoryId,
        formRecommendCategoryId = recommendCategoryId,
        formIsHot = isHot,
        formIsSale = isSale,
        formIsGroup = isGroup,
        formIsBargain = isBargain,
        formIsReleasePoints = isReleasePoints,
        formIsTimeLimit = isTimeLimit,
        formSkipCount = (pageIndex - 1) * pageSize,
        formMaxResultCount = pageSize,
        formIsWholesale = isWholesale;
      const items = yield request(
        api.ManageitemManageApiFetchParamCreactor.manageitemManageGetItemsAsync(
          {
            formStoreId,
            formKeywords,
            formSkipCount,
            formMaxResultCount,
            formItemCategoryId,
            formRecommendCategoryId,
            formIsHot,
            formIsWholesale,
            formIsSale,
            formIsGroup,
            formIsTimeLimit,
            formIsBargain,
            formIsReleasePoints
          }
        )
      );
      yield put({
        type: "save",
        payload: {
          items,
          itemSearchs: {
            storeId,
            keywords,
            pageIndex,
            pageSize,
            itemCategoryId,
            recommendCategoryId,
            isHot,
            isSale,
            isGroup,
            isTimeLimit,
            isWholesale,
            isBargain,
            isReleasePoints
          }
        }
      });
    },
    /**
     * 上架
     */
    *itemSale({ payload }, { call, put }) {
      const { form, callback } = payload;
      yield request(
        api.ManageitemManageApiFetchParamCreactor.manageitemManageSetItemSaleAsync(
          { form }
        )
      );
      callback && callback(true);
    },
    /**
     * 获取店铺推荐类型列表
     * @param param0
     * @param param1
     */
    *getRecommendCategories({ payload }, { call, put }) {
      const { storeId } = payload;
      if (storeId == "请选择") return;
      const formStoreId = storeId;
      const recommendCategories = yield request(
        api.ManageitemManageApiFetchParamCreactor.manageitemManageGetRecommendCategoriesAsync(
          { formStoreId }
        )
      );
      yield put({ type: "save", payload: { recommendCategories } });
    },
    /**
     * 获取推荐类型
     * @param param0
     * @param param1
     */
    *getRecommendCategory({ payload }, { call, put }) {
      const { id } = payload;
      const formId = id;
      const recommendCategory = yield request(
        api.ManageitemManageApiFetchParamCreactor.manageitemManageGetRecommendCategoryAsync(
          { formId }
        )
      );
      yield put({ type: "save", payload: { recommendCategory } });
    },
    /**
     * 保存推荐类型
     * @param param0
     * @param param1
     */
    *saveRecommendCategory({ payload }, { call, put }) {
      const { form, callback } = payload;
      yield request(
        api.ManageitemManageApiFetchParamCreactor.manageitemManageSaveRecommendCategoryAsync(
          { form }
        )
      );
      callback && callback(true);
    },
    /**
     * 移除推荐类型
     * @param param0
     * @param param1
     */
    *removeRecommendCategory({ payload }, { call, put }) {
      const { form, callback } = payload;
      yield request(
        api.ManageitemManageApiFetchParamCreactor.manageitemManageRemoveRecommendCategoryAsync(
          { form }
        )
      );
      callback && callback(true);
    },
    /**
     * 上移推荐类型
     * @param param0
     * @param param1
     */
    *moveUpRecommendCategory({ payload }, { call, put }) {
      const { form, callback } = payload;
      yield request(
        api.ManageitemManageApiFetchParamCreactor.manageitemManageMoveUpRecommendCategoryAsync(
          { form }
        )
      );
      callback && callback(true);
    },
    /**
     * 下移推荐类型
     * @param param0
     * @param param1
     */
    *moveDownRecommendCategory({ payload }, { call, put }) {
      const { form, callback } = payload;
      yield request(
        api.ManageitemManageApiFetchParamCreactor.manageitemManageMoveDownRecommendCategoryAsync(
          { form }
        )
      );
      callback && callback(true);
    },
    /**
     * 获取商品规格列表
     * @param param0
     * @param param1
     */
    *getItemSpecs({ payload }, { call, put }) {
      const { id } = payload;
      const formId = id;
      const itemSpecs = yield request(
        api.ManageitemManageApiFetchParamCreactor.manageitemManageGetItemSpecsAsync(
          { formId }
        )
      );
      yield put({ type: "save", payload: { itemSpecs } });
    },
    /**
     * 获取商品规格详情
     * @param param0
     * @param param1
     */
    *getItemSpec({ payload }, { call, put }) {
      const { id } = payload;
      const formId = id;
      const itemSpec = yield request(
        api.ManageitemManageApiFetchParamCreactor.manageitemManageGetItemSpecAsync(
          { formId }
        )
      );
      yield put({ type: "save", payload: { itemSpec } });
    },
    /**
     * 保存商品规格
     * @param param0
     * @param param1
     */
    *saveItemSpec({ payload }, { call, put }) {
      const { form, callback } = payload;
      yield request(
        api.ManageitemManageApiFetchParamCreactor.manageitemManageSaveItemSpecAsync(
          { form }
        )
      );
      callback && callback(true);
    },
    /**
     * 移除商品规格
     * @param param0
     * @param param1
     */
    *removeItemSpec({ payload }, { call, put }) {
      const { form, callback } = payload;
      yield request(
        api.ManageitemManageApiFetchParamCreactor.manageitemManageRemoveItemSpecAsync(
          { form }
        )
      );
      callback && callback(true);
    },
    /**
     * 获取商品评论列表
     * @param param0
     * @param param1
     */
    *getItemComments({ payload }, { call, put }) {
      const { id } = payload;
      const { itemId, pageIndex, pageSize, star, beginDate, endDate } = payload;
      const formItemId = itemId,
        formSkipCount = (pageIndex - 1) * pageSize,
        formMaxResultCount = pageSize,
        formStar = star,
        formBeginDate = beginDate,
        formEndDate = endDate;
      const itemComments = yield request(
        api.ManageitemManageApiFetchParamCreactor.manageitemManageGetItemCommentsAsync(
          {
            formItemId,
            formSkipCount,
            formMaxResultCount,
            formStar,
            formBeginDate,
            formEndDate
          }
        )
      );
      yield put({
        type: "save",
        payload: { itemComments, itemCommentSearchs: { pageIndex, pageSize } }
      });
    },
    /**
     * 回复商品评论
     * @param param0
     * @param param1
     */
    *setItemReplyComment({ payload }, { call, put }) {
      const { form, callback } = payload;
      yield request(
        api.ManageitemManageApiFetchParamCreactor.manageitemManageSetItemCommentReplyAsync(
          { form }
        )
      );
      callback && callback(true);
    },

    /**
     * 移除商品规格
     * @param param0
     * @param param1
     */
    *removeItemComment({ payload }, { call, put }) {
      const { form, callback } = payload;
      yield request(
        api.ManageitemManageApiFetchParamCreactor.manageitemManageRemoveItemCommentAsync(
          { form }
        )
      );
      callback && callback(true);
    },

    /**
     * 保存商品sku类别
     * @param param0
     * @param param1
     */
    *saveItemSkuCategory({ payload }, { call, put }) {
      const { form, callback } = payload;
      yield request(
        api.ManageitemManageApiFetchParamCreactor.manageitemManageSaveItemSkuCategoryAsync(
          { form }
        )
      );
      callback && callback(true);
    },
    /**
     * 保存商品sku
     * @param param0
     * @param param1
     */
    *saveItemSku({ payload }, { call, put }) {
      const { form, callback } = payload;
      yield request(
        api.ManageitemManageApiFetchParamCreactor.manageitemManageSaveItemSkuAsync(
          { form }
        )
      );
      callback && callback(true);
    },
    /**
     * 移除商品sku类别
     * @param param0
     * @param param1
     */
    *removeItemSkuCategory({ payload }, { call, put }) {
      const { form, callback } = payload;
      yield request(
        api.ManageitemManageApiFetchParamCreactor.manageitemManageRemoveItemSkuCategoryAsync(
          { form }
        )
      );
      callback && callback(true);
    },
    /**
     * 移除商品类别
     * @param param0
     * @param param1
     */
    *removeItemSku({ payload }, { call, put }) {
      const { form, callback } = payload;
      yield request(
        api.ManageitemManageApiFetchParamCreactor.manageitemManageRemoveItemSkuAsync(
          { form }
        )
      );
      callback && callback(true);
    },
    /**
     * 获取商品sku列表
     * @param param0
     * @param param1
     */
    *getItemSkuSpecs({ payload }, { call, put }) {
      const { id } = payload;
      const formId = id;
      const itemSkuSpecs = yield request(
        api.ManageitemManageApiFetchParamCreactor.manageitemManageGetItemSkuSpecsAsync(
          { formId }
        )
      );
      yield put({ type: "save", payload: { itemSkuSpecs } });
    },
    /**
     * 获取商品sku列表
     * @param param0
     * @param param1
     */
    *getItemSkus({ payload }, { call, put }) {
      const { id } = payload;
      const formId = id;
      const itemSkus = yield request(
        api.ManageitemManageApiFetchParamCreactor.manageitemManageGetItemSkusAsync(
          { formId }
        )
      );
      yield put({ type: "save", payload: { itemSkus } });
    },
    /**
     * 查询经营范围
     * @param param0
     * @param param1
     */
    *getBusinessScope({ payload }, { call, put }) {
      const { keywords, pageIndex, pageSize } = payload;
      const formKeywords = keywords,
        formSkipCount = (pageIndex - 1) * pageSize,
        formMaxResultCount = pageSize;
      const businessScopes = yield request(
        api.ManageitemManageApiFetchParamCreactor.manageitemManageGetAllBusinessScopeAsync(
          { formKeywords, formSkipCount, formMaxResultCount }
        )
      );
      yield put({
        type: "save",
        payload: {
          businessScopes,
          businessScopeSearchs: { keywords, pageIndex, pageSize }
        }
      });
    },
    /**
     * 保存经营范围
     * @param param0
     * @param param1
     */
    *saveBusinessScope({ payload }, { call, put, select }) {
      const { form, callback } = payload;
      const { businessScopeSearchs } = yield select(({ item }) => item);
      yield request(
        api.ManageitemManageApiFetchParamCreactor.manageitemManageSaveBuusinessScopeAsync(
          { form }
        )
      );
      yield put({
        type: "getBusinessScope",
        payload: { ...businessScopeSearchs }
      });
      callback && callback(true);
    },
    /**
     * 获取分红比例列表信息
     * @param param0
     * @param param1
     */
    *getDividends({ payload }, { call, put }) {
      const { pageIndex, pageSize } = payload;
      const formSkipCount = (pageIndex - 1) * pageSize,
        formMaxResultCount = pageSize;
      const dividends = yield request(
        api.ManagedividendManageApiFetchParamCreactor.managedividendManageGetDividendsAsync(
          {
            formSkipCount,
            formMaxResultCount
          }
        )
      );
      yield put({
        type: "save",
        payload: { dividends, dividendSearchs: { pageIndex, pageSize } }
      });
    }
  },
  reducers: {
    save(state, action) {
      return { ...state, ...action.payload };
    }
  },
  subscriptions: {
    setup({ dispatch, history }) {}
  }
};
